package com.vstudy.zx.dao;

import com.vstudy.zx.dao.provider.TeacherDaoProvider;
import com.vstudy.zx.entity.Teacher;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TeacherDao {
    @Select("select * from tb_teacher where te_id = #{teId}")
    Teacher selectById(Long teId);

    @Select("select * from tb_teacher where te_id = #{teId}")
    @Results({
            @Result(column = "school_id", property = "school",
                    one = @One(select = "com.vstudy.zx.dao.SchoolDao.selectById"))
    })
    Teacher selectByIdCasSchool(Long teId);

    @Update("update tb_teacher set te_password=#{te_password} where te_id=#{te_id}")
    int updatePwd(Teacher teacher);

    // 根据登录名查询用户
    @Select("select * from tb_teacher where te_login_name = #{loginName}")
    @Results({
            @Result(column = "school_id", property = "school",
                    one = @One(select = "com.vstudy.zx.dao.SchoolDao.selectById"))
    })
    Teacher select(@Param("loginName") String loginName);

    // 根据登录名查询用户, 判断用户名是否存在
    @Select("select * from tb_teacher where te_login_name = #{loginName} and te_id<>#{te_id}")
    Teacher selectByLoginName(@Param("loginName") String loginName, @Param("te_id") Long excludeId);

    @Update("update tb_teacher set te_image=#{te_image} where te_id=#{te_id}")
    int updateImg(Teacher teacher);

    @UpdateProvider(type = TeacherDaoProvider.class, method = "update")
    int update(Teacher teacher);

    @Select("select * from tb_teacher where te_login_name like concat('%',#{key},'%') " +
            "or te_name like concat('%',#{key},'%') ")
    @Results({
            @Result(column = "school_id", property = "school",
                    one = @One(select = "SchoolDao.selectById"))
    })
    List<Teacher> selectByKeyLoginName(String key);

    @Insert("insert into tb_teacher(te_name, te_login_name, te_password) " +
            "values(#{realName},#{username},#{password})")
    int add(@Param("realName") String realName, @Param("username") String username, @Param("password") String password);
}
